<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/core" prefix="c"%>
<%@ taglib uri="/tags/fmt" prefix="fmt"%>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--

		function validateForm(){
			var errorLen  = '';
			var transport = document.getElementById('view.purchaseOrderTransportId');
			var startwith = document.getElementById('view.startWith');			
			var periodicity = document.getElementById('view.purchaseOrderPeriodicity');
			var tcsize = document.getElementById('transportChargesSize');
			
			if(transport.value == '-1'){
				errorLen = errorLen + '<bean:message key="error.message.purchaseorder.select.tranport"/>\n';
			}
			if(startwith.value == '' || (isNaN(startwith.value)) ||  startwith.value <= 0){
				errorLen = errorLen + '<bean:message key="error.message.purchaseorder.wrong.start"/>\n';
			}
	
			if(periodicity.value == '' || (isNaN(periodicity.value)) || periodicity.value == 0){
				errorLen = errorLen + '<bean:message key="error.message.purchaseorder.wrong.periodicity"/>\n';
			}
			
			if(tcsize!=null && tcsize.value > 0){
	    		for(i = 0; i<tcsize.value; i = i+1 ){
	    			var tc = document.getElementById("tc" + i )
	    			if((tc.value == "" ) ||  (tc.value == "0")){
	    				errorLen = errorLen + '<bean:message key="warning.message.purchaseorder.invalid.pricePerTon"/>\n';
	    				break;
	    			}
	    			if(!(tc.value == "") && (isNaN(tc.value))){
	    				errorLen = errorLen + '<bean:message key="warning.message.purchaseorder.invalid.pricePerTon"/>\n';
	    				break;
	    			}
	    		}
	    	}
			
			return errorLen;
		}
		
		
		function submitForm(action){			
			var form = document.getElementById('mainForm');
	    	form.action = action;

	    	if(action == 'purchaseProgramGenerate.do' || action == 'pupPreGenerate.do'){
	    		var errors = validateForm();
	    		if( errors == '' ){
	    			form.submit();
	    		}else{
    				alert('Errores :  \n' + errors);
	    		}
	    	}else{
	    		form.submit();
	    	}	
		}
//-->
</script>
</head>

<body>
<html:form action="/purchaseProgramGenerate" styleId="mainForm">
<table width="75%" >
  <tr> 
    <td rowspan="2">
    	<table width="75%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
        <tr> 
          <td width="50%" class="etiqueta"><bean:message key="label.program.purchase.transport"/></td>
          <td width="50%" class="etiqueta_campo">
          	<html:select property="view.purchaseOrderTransportId" styleId="view.purchaseOrderTransportId" style="width:160px" styleClass="campo_texto" onchange="javascript:submitForm('pupLoadCostsForTransport.do')" >
          		<html:option value="-1"><bean:message key="label.program.purchase.selectanoption"/></html:option>
          		<html:optionsCollection name="PurchaseProgramGeneratorForm" property="transports" value="transportIdentifier" label="transportDescription"/>
       		</html:select>          
          </td>
        </tr>
        <tr> 
          <td width="50%" class="etiqueta"><bean:message key="label.program.purchase.corntype"/></td>
          <td width="50%" class="etiqueta_campo">
          	<html:select property="view.cornTypeId" styleId="view.cornTypeId" style="width:160px" styleClass="campo_texto">          		
          		<html:optionsCollection name="PurchaseProgramGeneratorForm" property="cornTypes" value="cornTypeId" label="description"/>
       		</html:select>          
          </td>
        </tr>
        
        <tr> 
          <td class="etiqueta"><bean:message key="label.program.purchase.startwith"/></td>
          <td class="etiqueta_campo"> 
          <html:text 
          property="view.startWith" 
          styleId="view.startWith" 
          style="width:160px" 
          styleClass="columnanumero" 
          onfocus="this.select()"  
          onkeydown="return (numeric(event,0) && noPaste(event, this));"/></td>
        </tr>
        <tr> 
          <td  class="etiqueta"><bean:message key="label.program.purchase.destination"/></td>
          <td  class="etiqueta_campo">
          	<html:select property="view.purchaseOrderPlantId" styleId="view.purchaseOrderPlantId" style="width:160px" styleClass="campo_texto">
          		<html:option value="-1"><bean:message key="label.program.purchase.selectanoption"/></html:option>
          		<html:optionsCollection name="PurchaseProgramGeneratorForm" property="plants" value="plantId" label="shortName"/>
       		</html:select>
          </td>
        </tr>
        <tr> 
          <td class="etiqueta"><bean:message key="label.program.purchase.periodicity"/></td>
          <td class="etiqueta_campo">
          <html:text 
          property="view.purchaseOrderPeriodicity" 
          styleId="view.purchaseOrderPeriodicity" 
          style="width:160px" 
          styleClass="columnanumero" 
          onfocus="this.select()" 
		  onkeydown="return (numeric(event,0) && noPaste(event, this));"/>
		  <html:select property="view.typeEnableWeek" styleClass="campo_texto">
		  	<html:option value="0"><bean:message key="label.program.purchase.everyBiweek"/></html:option>
		  	<html:option value="1"><bean:message key="label.program.purchase.firstBiweek"/></html:option>
		  	<html:option value="2"><bean:message key="label.program.purchase.secondBiweek"/></html:option>
		  </html:select>
		  </td>
        </tr>
        <tr> 
          <td class="etiqueta"><bean:message key="label.program.purchase.initdate"/></td>
          <td class="etiqueta_campo">
          	<html:select property="view.biWeekInit" styleId="view.biWeekInit"  style="width:160px" styleClass="campo_texto" >
          		<html:optionsCollection  name="PurchaseProgramGeneratorForm" property="biweeks" value="biweekPeriodId" label="description" />
       		</html:select>
          </td>
        </tr>
        <tr> 
          <td class="etiqueta"><bean:message key="label.program.purchase.finishdate"/></td>
          <td class="etiqueta_campo"> 
          	<html:select property="view.biWeekFinish" styleId="view.biWeekFinish" style="width:160px" styleClass="campo_texto">
          		<html:optionsCollection name="PurchaseProgramGeneratorForm" property="biweeks" value="biweekPeriodId" label="description"/>
       		</html:select>															
            </td>
        </tr>
      </table></td>
    <td valign="top" style="border: 1px;">
    	<table width="100%" cellpadding="1" cellspacing="1" border="0" bgcolor="#CCCCC0">
        	<tr class="titulocolumna" > 
          		<td ><bean:message key="label.program.purchase.costs"/></td>
          		<td ><bean:message key="label.program.purchase.priceperton"/></td>
        	</tr>

			<c:forEach var="cost"
				items="${PurchaseProgramGeneratorForm.transportCharges}"
				varStatus="loop">
				
        	<tr class="columnadatoPar">         	
          		<td class="columnadato">
          		<input type="hidden" id='transportChargesSize' value='<c:out value="${PurchaseProgramGeneratorForm.transportChargesSize}"/>' />
          		
          			<bean:write name="cost" property="costElementDescription" />
          		</td>
          		<td class="columnanumero">        
          		 	<input value='<fmt:formatNumber pattern="###,###,####0.0000" type="number" maxFractionDigits="4" value="${cost.transportChargeAmount}"/>' 
						  class="columnanumero"
						  onfocus="this.select();" 
						  name="transportCharges[<c:out value="${loop.index}"/>].transportChargeAmount"
						  onkeydown="return (numeric(event, 1) && noPaste(event, this));" 
						  onkeyup="extractNumber(this,4,false);" 
						  onclick="this.focus()" 
						  style="text-align:right"
						  id='tc<c:out value="${loop.index}"/>'/>          			
          		</td>
        	</tr>
			</c:forEach>        	
        	

      </table>
    </td>
  </tr>
  <tr> 
    <td align="center" valign="middle"> 
      <input 
      	type="button" 
      	name="Submit" 
      	value="<bean:message 
      	key="label.program.purchase.pregeneration"/>" 
      	class="boton_large" 
      	nowrap="true"
      	onclick="javascript:submitForm('pupPreGenerate.do')"
      />

    </td>
  </tr>
</table>

<table width="75%" cellpadding="1" cellspacing="1" border="0" bgcolor="#CCCCC0">
  <tr class="titulocolumna">
    <td ><bean:message key="label.program.purchase.code"/></td>
    <td ><bean:message key="label.program.purchase.purchasedate"/></td>
    <td ><bean:message key="label.program.purchase.destination"/></td>
  </tr>
  
  <c:forEach var="purchaseOrder" items="${PurchaseProgramGeneratorForm.purchaseOrderViews}" varStatus="loop" >
 <tr class="columnadatoPar">
		<td>
			<bean:write name="purchaseOrder" property="purchaseOrderCode" /><bean:write name="purchaseOrder" property="postFix" />
		</td>

 
    <td>
	<select name="purchaseOrderViews[<c:out value="${loop.index}"/>].purchaseOrderWeekPeriodId" class="campo_texto">
    <c:forEach var="biweek" items="${PurchaseProgramGeneratorForm.biweeksRange}" varStatus="loop2" >
    	<option     			
    	    	<c:if test="${purchaseOrder.biWeekInit== biweek.biweekPeriodId }"> 
    				selected='true'
    			</c:if>
    	
    			value='<bean:write name="biweek" property="biweekPeriodId" />'><bean:write name="biweek" property="description" />    			
		</option>
    </c:forEach>
	</select>
    
    </td>
    <td>
	<select name="purchaseOrderViews[<c:out value="${loop.index}"/>].purchaseOrderPlantId" class="campo_texto">
	<option value="-1"><bean:message key="label.program.purchase.selectanoption"/></option>    
    <c:forEach var="plant" items="${PurchaseProgramGeneratorForm.plants}" varStatus="loop3" >
    	<option 
    	<c:if test="${plant.plantId == PurchaseProgramGeneratorForm.view.purchaseOrderPlantId }"> 
    		selected='true'
    	</c:if>
    	value='<bean:write name="plant" property="plantId" />'><bean:write name="plant" property="shortName" /></option>
    </c:forEach>
	</select>    
    </td>

  </tr>
  </c:forEach>
</table>
<p>&nbsp;</p>
<table width="75%" style="border-style:solid; border-width:1px; border-color: #cacaca" cellspacing="1">
  <tr>
    <td align="center" valign="middle">
    <input 
    type="button" 
    name="Submit2"
   	onclick="javascript:submitForm('purchaseProgramGenerate.do')" 
    value="<bean:message key="label.program.purchase.save"/>" class="boton"></td>
  </tr>
</table>
<p><br/>
</p>
</html:form>
</body>
</html>
